<template>
    <div>
        <h1>会员增加</h1>
        {{info}}
        <table class="table table-database">
            <tbody>
                <tr>
                    <td align="right">会员编号</td>
                    <td>
                        <input type="text" v-model="info.code" disabled>
                    </td>
                </tr>

                <tr>
                    <td align="right">会员姓名</td>
                    <td>
                        <input type="text" v-model="info.userName" >
                    </td>
                </tr>

                <tr>
                    <td align="right">会员手机号</td>
                    <td>
                        <input type="text" v-model="info.tel" maxlength="11" >
                    </td>
                </tr>

                <tr>
                    <td align="right">会员身份证</td>
                    <td>
                        <input type="text" v-model="info.card" maxlength="18">
                    </td>
                </tr>

                <tr>
                    <td align="right">会员生日</td>
                    <td>
                        <input type="date" v-model="info.birthday" >
                    </td>
                </tr>

                <tr>
                    <td align="right">会员性别</td>
                    <td>
                        <input type="radio" v-model="info.gender" name="gender" :value="1" >男
                        <input type="radio" v-model="info.gender" name="gender" :value="0" >女
                    </td>
                </tr>

                <tr>
                    <td align="right">会员爱好</td>
                    <td>
                        <input type="checkbox" v-model="info.likes" name="likes" value="吃" >吃
                        <input type="checkbox" v-model="info.likes" name="likes" value="喝" >喝
                        <input type="checkbox" v-model="info.likes" name="likes" value="玩" >玩
                        <input type="checkbox" v-model="info.likes" name="likes" value="乐" >乐
                    </td>
                </tr>


                <tr>
                    <td align="right">会员等级</td>
                    <td>
                        <select v-model="info.vipLevelId">
                            <option value="">请选择</option>
                            <option :value="item.id" v-for="item in levelList" :key="item.id">{{ item.vipLevelName }}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">会员积分</td>
                    <td>
                        <input type="text" v-model="info.score">
                    </td>
                </tr>

                <tr>
                    <td align="right">会员余钱</td>
                    <td>
                        <input type="text" v-model="info.money">
                    </td>
                </tr>

                <tr>
                    <td align="right">会员简介</td>
                    <td>
                        <input type="text" v-model="info.desc">
                    </td>
                </tr>
                
                <tr>
                    <td align="right">同意条款</td>
                    <td>
                        <input type="checkbox" v-model="info.isAgree">xxx条款
                    </td>
                </tr>

            
                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="增加" @click="ok" >
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import moment from 'moment';
import { ref,reactive,onMounted } from 'vue';
import { useRouter } from 'vue-router';
const router=useRouter();


let info:any=reactive({
  code:  moment(new Date()).format('yyyyMMDD')+Math.random().toString().substring(2,5),
  userName:  '',
  tel: '',
  card: '',
  birthday: '',
  gender: 0,
  likes:[],
  vipLevelId: '',
  score: '',
  money: '',
  desc: '',
  isAgree: '',
});
let levelList:any=ref([]);

onMounted(()=>{
    getVipLevelList();
});
const getVipLevelList=()=>{
    axios({
        url:'https://localhost:7080/api/Vip/GetVipLevelList',
        method:'get',
    })
    .then((res)=>{
        console.log(res);
        levelList.value=res.data;
    })
    .catch((err)=>{
        console.log(err);
    });
};
const ok=()=>{
    if(info.userName==''){
        alert('名称不为空');
        return;
    }
    if(info.tel==''){
        alert('手机不为空');
        return;
    }
    let reg=/^1[3-9]\d{9}$/
    if(reg.test(info.tel)==false){
        alert('手机不合法');
        return;
    }
    if(info.card==''){
        alert('身份证不为空');
        return;
    }
    let reg1=/^\d{17}[0-9xX]$/
    if(reg1.test(info.card)==false){
        alert('身份证不合法');
        return;
    }
    if(info.score==''){
        alert('积分不为空');
        return;
    }
    let reg2=/^[1-9]\d*$/
    if(reg2.test(info.score)==false){
        alert('积分正整数');
        return;
    }
    if(info.money==''){
        alert('余钱不为空');
        return;
    }
     let reg3=/^[1-9]\d*(\.\d+)?$/

    if(reg3.test(info.money)==false){
        alert('余钱要正数');
        return;
    }
    if(info.desc==''){
        alert('简介不为空');
        return;
    }
info.likes=info.likes.toString();
//console.log(info.value)
axios({
    method:'post',
    url:'https://localhost:7080/api/Vip/AddVip',
    data:info,
})
.then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert('手机号重复');
            return;
        }
        else{
            alert('添加成功');
            router.push('/show');
        }
    })
    .catch((err)=>{
        console.log(err);
    });
};

</script>

<style scoped>

</style>